package com.yxjx.duoxue;

import java.util.ArrayList;

import org.xmlpull.v1.XmlPullParser;

import com.yxjx.duoxue.BaseActionBarActivity.Observee;
import com.yxjx.duoxue.course.CourseIntroductionActivity;
import com.yxjx.duoxue.course.SchoolIntroductionActivity;
import com.yxjx.duoxue.course.TeacherIntroductionActivity;
import com.yxjx.duoxue.course.YxItemList;
import com.yxjx.duoxue.course.YxItemList.YxItemListAdapter;
import com.yxjx.duoxue.customview.InnerScrollView;
import com.yxjx.duoxue.customview.InnerWebView;
import com.yxjx.duoxue.customview.OutScrollView;
import com.yxjx.duoxue.customview.YxDetailedInfoView;
import com.yxjx.duoxue.customview.YxDetailedInfoView.YxListAdapter;
import com.yxjx.duoxue.customview.YxDetailedInfoView1;
import com.yxjx.duoxue.customview.YxDetailedInfoView1.YxListAdapter1;
import com.yxjx.duoxue.customview.YxFoldableListView;
import com.yxjx.duoxue.customview.YxFoldableListView.FoldableAdapter;
import com.yxjx.duoxue.customview.YxListView;
import com.yxjx.duoxue.datamodel.DMTeacher;
import com.yxjx.duoxue.datamodel.course.DMCourse;
import com.yxjx.duoxue.utils.Utils;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

/**
 * 显示内嵌的H5页面或者内嵌的scroll页面
 * @author mengmengchen
 *
 */
public class YxSegment extends Fragment {
	
	public static final String KEY_SEGMENT_TYPE = "KEY_SEGMENT_TYPE";
	public static final String KEY_LAYOUT_ID = "KEY_LAYOUT_ID";
	public static final String KEY_URL = "KEY_URL";
	private Context mContext;
	
	@Override
	public View onCreateView(LayoutInflater inflater,
			@Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
		int layoutId = getArguments().getInt(KEY_LAYOUT_ID);
		View ret = inflater.inflate(layoutId, container, false);
		mContext = ret.getContext();
		OutScrollView outer = (OutScrollView) container.getRootView().findViewById(R.id.scrollView);
		if (layoutId == R.layout.segment_h5) {
			InnerWebView inner = (InnerWebView) ret.findViewById(R.id.inner);
			inner.parentScrollView = outer;
			inner.getSettings().setAppCacheEnabled(true);
			inner.getSettings().setBuiltInZoomControls(false);
	        inner.getSettings().setJavaScriptEnabled(true);
	        String url = getArguments().getString(KEY_URL);
	        inner.loadUrl(url);
		} else if (layoutId == R.layout.segment_teacher_list) {
			YxItemList mTeachers = (YxItemList) ret.findViewById(R.id.inner);
			mTeachers.parentScrollView = outer;
			final YxItemListAdapter mTearchersAdapter;
			
			mTearchersAdapter = mTeachers.new YxItemListAdapter();
			mTeachers.setAdapter(mTearchersAdapter);
			
			// customize teachers
			if (getActivity() instanceof CourseIntroductionActivity) {
				final CourseIntroductionActivity activity = (CourseIntroductionActivity) getActivity();
				activity.setTeachersDataChangeNotifier(new Observee() {
					@Override
					public void notifyDataChanged() {
						refreshItemList(mTearchersAdapter, activity.getTeachers());
					}
				});
				refreshItemList(mTearchersAdapter, activity.getTeachers());
			} else if (getActivity() instanceof SchoolIntroductionActivity) {
				final SchoolIntroductionActivity activity = (SchoolIntroductionActivity) getActivity();
				activity.setTeachersDataChangeNotifier(new Observee() {
					@Override
					public void notifyDataChanged() {
						refreshItemList(mTearchersAdapter, activity.getTeachers());
					}
				});
				refreshItemList(mTearchersAdapter, activity.getTeachers());
			}
		} else if (layoutId == R.layout.segment_course_list) {
			YxItemList mCourseList = (YxItemList) ret.findViewById(R.id.inner);
			mCourseList.parentScrollView = outer;
			
			final YxItemListAdapter mCourseListAdapter = mCourseList.new YxItemListAdapter();
			mCourseList.setAdapter(mCourseListAdapter);
			
			// customize courses
			if (getActivity() instanceof SchoolIntroductionActivity) {
				final SchoolIntroductionActivity activity = (SchoolIntroductionActivity) getActivity();
				activity.setCoursesDataChangeNotifier(new Observee() {

					@Override
					public void notifyDataChanged() {
						refreshItemList(mCourseListAdapter, activity.getCourses());
					}
				});
				refreshItemList(mCourseListAdapter, activity.getCourses());
			} else if (getActivity() instanceof TeacherIntroductionActivity) {
				final TeacherIntroductionActivity activity = (TeacherIntroductionActivity) getActivity();
				activity.setCoursesDataChangeNotifier(new Observee() {
					
					@Override
					public void notifyDataChanged() {
						refreshItemList(mCourseListAdapter, activity.getCourses());
						
					}
				});
				refreshItemList(mCourseListAdapter, activity.getCourses());
			}
		}
		return ret;
	}

	
	private void refreshItemList(YxItemListAdapter coursesAdapter,
			ArrayList<? extends Object> items) {
		if (items == null) {
			return;
		}
		coursesAdapter.setData(items);
		
		View rootView = getView();
		if (rootView == null) {
			return;
		}
		
		View defaultPage = rootView.findViewById(R.id.default_page);
		if (items != null && items.size() > 0) {
			if (defaultPage != null) {
				defaultPage.setVisibility(View.INVISIBLE);
			}
		} else {
			if (defaultPage != null) {
				defaultPage.setVisibility(View.VISIBLE);
			}
		}
	}
	
	private void setWebViewUrl() {
		
	}

	public static YxSegment getSegment(int layoutId, Bundle extra) {
		YxSegment seg = new YxSegment();
		Bundle bundle = new Bundle(extra);
		bundle.putInt(YxSegment.KEY_LAYOUT_ID, layoutId);
		seg.setArguments(bundle);

		return seg;
	}

}
